package com.capybara.account.config;

import com.capybara.account.base.MyHandlerExceptionResolvers;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * MVC 配置
 *
 * @author guochaowang
 * @date 2024/09/14
 */
@Slf4j
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {

    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
    private static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 文件路径
     */
    @Value("${storage.local.path:#{systemProperties['app.path']}")
    private String filePath;

    /**
     * 添加资源处理程序
     *
     * @param registry 注册表
     */
    @Override
    public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
        if (filePath.contains("/")) {
            registry.addResourceHandler("/static/**").addResourceLocations("file:" + filePath + "/static/");
        } else {
            registry.addResourceHandler("/static/**").addResourceLocations("file:" + filePath + "\\static\\");
        }
        super.addResourceHandlers(registry);
    }




    /**
     * 统一异常处理
     *
     * @param exceptionResolvers 异常解决程序
     */
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        exceptionResolvers.add(new MyHandlerExceptionResolvers());
    }


    /**
     * 添加格式化程序
     *
     * @param registry 注册表
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
        registry.addConverter(String.class, LocalDate.class, s -> LocalDate.parse(s, dateFormatter));
        registry.addConverter(String.class, LocalDateTime.class, s -> LocalDateTime.parse(s, datetimeFormatter));
    }

    /**
     * 配置消息转换器
     *
     * @param converters 变换 器
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .indentOutput(true)
                .dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
                .simpleDateFormat(DATE_TIME_FORMAT)
                .serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)))
                .serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
                .deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)))
                .deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
                .modulesToInstall(new ParameterNamesModule());
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
}
